#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

void *fun(void *);

int main(int argc, char *argv[]) {
  pthread_t tid;

  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

  pthread_create(&tid, &attr, fun, NULL);
  sleep(2);
  pthread_cancel(tid);

  pthread_attr_destroy(&attr);
  pthread_exit(NULL);
}

void *fun(void *argv) {

  int i = 0;
  while (i < 5) {
    printf("I am son %d\n", i);
    i++;
    sleep(1);
  }
  pthread_exit(NULL);
}
